package com.ex.mzwqz.activity;

import android.annotation.TargetApi;
import android.graphics.Bitmap;
import android.os.Build;
import android.view.KeyEvent;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;

import com.ex.mzwqz.AppContext;
import com.ex.mzwqz.R;
import com.ex.mzwqz.util.Constants;

import java.lang.reflect.InvocationTargetException;


/**
 * @author ExDog love_mobile@163.com
 * @ClassName: WebViewActivity
 * @Description: 重写web 加速网页加载速度
 * @date 2014-4-8 下午7:00:31
 */
public class WebViewActivity extends BaseActivity {

    private WebView mWebView;

    private View vLoading;

    //  private TextView tvTitle;

    // private ImageView ivBack;

    private ImageView ivLoading;

    private Animation animCircle;
    private String url;


    @Override
    protected int setContentViewResId() {
        return R.layout.activity_webview;
    }

    @Override
    public void initView() {
        mWebView = (WebView) findViewById(R.id.webview);
        vLoading = (View) findViewById(R.id.loading_box);
        ivLoading = (ImageView) findViewById(R.id.loading);
    }

    @Override
    protected void initEvent() {

    }


    @TargetApi(Build.VERSION_CODES.FROYO)
    @Override
    public void initData() {
        setTitle(getIntent().getStringExtra(Constants.KEY_WEBVIEW_TITLE));
        animCircle = AnimationUtils.loadAnimation(this, R.anim.circle);
        mWebView.getSettings().setBuiltInZoomControls(true);
        mWebView.getSettings().setJavaScriptEnabled(true);
        // mWebView.getSettings().setPluginsEnabled(true);
        mWebView.getSettings().setPluginState(WebSettings.PluginState.ON);
        mWebView.getSettings().setSupportZoom(true);
        mWebView.getSettings().setUseWideViewPort(true);
        mWebView.getSettings().setLoadWithOverviewMode(true);
        mWebView.getSettings().setRenderPriority(WebSettings.RenderPriority.HIGH);
        mWebView.getSettings().setBlockNetworkImage(true);
        mWebView.setWebChromeClient(new WebChromeClient());
        mWebView.setWebViewClient(new MyWebViewClient());
        url = getIntent().getStringExtra(Constants.KEY_WEBVIEW_URL);
        mWebView.loadUrl(url);
        AppContext.getInstance().synCookies(this,"http://wlmqmzw.qinqinz.com/");
      //  mWebView.loadUrl("http://1i28.l.mob.com/INCul");
    }


    private class MyWebViewClient extends WebViewClient {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            //method.endProgressDialog();
            return super.shouldOverrideUrlLoading(view, url);
        }

        @Override
        public void onPageFinished(WebView view, String url) {
            //method.endProgressDialog();
            //vLoading.setVisibility(View.GONE);
            mWebView.getSettings().setBlockNetworkImage(false);
            super.onPageFinished(view, url);
        }

        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {

            super.onPageStarted(view, url, favicon);
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            try {
                Class.forName("android.webkit.WebView")
                        .getMethod("onPause", (Class[]) null)
                        .invoke(mWebView, (Object[]) null);
            } catch (IllegalArgumentException e) {
                e.printStackTrace();
            } catch (SecurityException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            } catch (NoSuchMethodException e) {
                e.printStackTrace();
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }
            this.finish();
        }
        return super.onKeyDown(keyCode, event);
    }
}



